/*
 * JMonthChooser.java
 *
 * Created on 14 de septiembre de 2008, 19:53
 */

package es.atareao.alejandria.gui;

import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.util.Date;
import es.atareao.alejandria.lib.DateUtils;

/**
 *
 * @author  Propietario
 */
public class JMonthChooser extends javax.swing.JPanel {
    private final static long serialVersionUID=0L;
    /** Creates new form JMonthChooser */
    public JMonthChooser() {
        initComponents();
        this.initMonths();
        this.setDate(DateUtils.Ahora());
        this.setMonth(DateUtils.monthOfYear(this.getDate()));
    }
    private void initMonths(){
        DateFormatSymbols symbols = new DateFormatSymbols();  
        String[] monthNames = symbols.getMonths();  
        this.setMonths(monthNames);
    }
    private void actualiza(){
        int number;
        if(this.JMontNumber.getValue()==null){
            number=0;
        }else{
            number=((Integer)this.JMontNumber.getValue());
        }
        Date oldDate=this.getDate();
        this.setMonth(number);
        Date newDate=this.getDate();
        this.firePropertyChange("SelectedMonthChanged", oldDate, newDate);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        JMonthText = new javax.swing.JTextField();
        JMontNumber = new javax.swing.JSpinner();

        setMaximumSize(new java.awt.Dimension(49150, 25));
        setMinimumSize(new java.awt.Dimension(120, 25));
        setPreferredSize(new java.awt.Dimension(120, 25));
        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.X_AXIS));

        JMonthText.setEditable(false);
        add(JMonthText);

        JMontNumber.setMaximumSize(new java.awt.Dimension(18, 25));
        JMontNumber.setMinimumSize(new java.awt.Dimension(18, 25));
        JMontNumber.setPreferredSize(new java.awt.Dimension(18, 25));
        JMontNumber.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                JMontNumberPropertyChange(evt);
            }
        });
        JMontNumber.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                JMontNumberStateChanged(evt);
            }
        });
        add(JMontNumber);
    }// </editor-fold>//GEN-END:initComponents

    private void JMontNumberStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_JMontNumberStateChanged
        actualiza();
    }//GEN-LAST:event_JMontNumberStateChanged

    private void JMontNumberPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_JMontNumberPropertyChange
        actualiza();
    }//GEN-LAST:event_JMontNumberPropertyChange
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSpinner JMontNumber;
    private javax.swing.JTextField JMonthText;
    // End of variables declaration//GEN-END:variables
    private String[] _months;
    private int _month;
    private Date _date;

    public int getMonth() {
        return _month;
    }

    public void setMonth(int month) {
        if(month<0){
            month=0;
        }else if(month>11){
            month=11;
        }            
        this._month = month;
        this.JMontNumber.setValue(month);
        this.JMonthText.setText(this.getMonths()[month]);
        int day = DateUtils.dayOfMonth(this.getDate());
        int year = DateUtils.year(this.getDate());
        try {
            this._date = DateUtils.toDate(day, month, year);
        } catch (ParseException ex) {
            ErrorDialog.manejaError(ex);
        }
    }

    public String[] getMonths() {
        return _months;
    }

    public void setMonths(String[] months) {
        this._months = months;
    }

    public Date getDate() {
        return _date;
    }

    public void setDate(Date date) {
        this._date = date;
        this.setMonth(DateUtils.monthOfYear(date));
    }
}
